﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Nova_DAL;
using System.Data.Common;
using Nova_Common;
using System.Text;


public partial class System_DepartMent_DepartMent : System.Web.UI.Page
{
    DataSet ds = new DataSet();
    Nova_MOD.DepartMent Nova_MOD_DepartMent = new Nova_MOD.DepartMent();
    Nova_BLL.DepartMent Nova_BLL_DepartMent = new Nova_BLL.DepartMent();
    protected int iCount = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        LoginLogic.IsLogin();


        if (!IsPostBack)
        {
            gv_Bind();
        }

        //验证TextBox
        string strJs = "";
        strJs = "return Validate(";
        strJs += "'txtvDepartmentNo','部门编号不为空','r'";
        strJs += ",'txtvDepartment','部门名称不为空','r'";
        strJs += ");";
        ibtnSubmit.Attributes.Add("onclick", strJs);
    }

    /// <summary>
    /// 部门列表
    /// </summary>
    protected void gv_Bind()
    {
        ds = Nova_BLL_DepartMent.DataListvDepartment();
        gvDepart.DataSource = ds;
        gvDepart.DataBind();
    }

    /// <summary>
    /// 添加部门
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnSubmit_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Nova_MOD_DepartMent.vDepartmentNo = txtvDepartmentNo.Text.ToString().Trim();
            Nova_MOD_DepartMent.vDepartment = txtvDepartment.Text.ToString().Trim();

            ds = Nova_BLL_DepartMent.DataListvDepartment("0", Nova_MOD_DepartMent.vDepartmentNo);
            
            if (ds.Tables[0].Rows.Count > 0)
            {
                ShowBox.MessageBox(this, "部门编号已存在！");
                txtvDepartmentNo.Text = "";
            }
            else
            {
                iCount = Nova_BLL_DepartMent.AddDepartMent(Nova_MOD_DepartMent);

                if (iCount > 0)
                {
                    gv_Bind();
                    ibtnReset_Click(sender, e);//清空
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

    /// <summary>
    /// 清空
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnReset_Click(object sender, ImageClickEventArgs e)
    {
        txtvDepartmentNo.Text = "";
        txtvDepartment.Text = "";
    }

    /// <summary>
    /// 取消
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvDepart_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvDepart.EditIndex = -1;
        gv_Bind();
    }

    /// <summary>
    /// 编辑
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvDepart_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvDepart.EditIndex = e.NewEditIndex;
        gv_Bind();
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvDepart_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string strID = gvDepart.DataKeys[e.RowIndex].Value.ToString();
        TextBox txt_vDepartmentNo = (TextBox)gvDepart.Rows[e.RowIndex].FindControl("txt_vDepartmentNo");
        TextBox txt_vDepartment = (TextBox)gvDepart.Rows[e.RowIndex].FindControl("txt_vDepartment");

        if (txt_vDepartmentNo.Text.Trim() == "")
        {
            ShowBox.MessageBox(this, "部门编号不为空！");
            return;
        }
        if (txt_vDepartment.Text.Trim() == "")
        {
            ShowBox.MessageBox(this, "部门名称不为空！");
            return;
        }

        try
        {
            ds = Nova_BLL_DepartMent.DataListvDepartment(strID, txt_vDepartmentNo.Text.ToString());
            iCount = ds.Tables[0].Rows.Count;
            if (iCount > 0)
            {
                ShowBox.MessageBox(this, "部门编号已存在！");
                txtvDepartmentNo.Text = "";
                return;
            }
            else
            {
                Nova_MOD_DepartMent.PID = strID;
                Nova_MOD_DepartMent.vDepartmentNo = txt_vDepartmentNo.Text.ToString().Trim();
                Nova_MOD_DepartMent.vDepartment = txt_vDepartment.Text.ToString().Trim();
                iCount = Nova_BLL_DepartMent.UpdateDepartMent(Nova_MOD_DepartMent);

                if (iCount > 0)
                {
                    gvDepart.EditIndex = -1;
                    gv_Bind();
                    return;
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnDelete_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton ibtnDelete = (ImageButton)sender;
        Nova_MOD_DepartMent.PID = ibtnDelete.CommandArgument.Trim();

        if (!string.IsNullOrEmpty(ibtnDelete.CommandArgument))
        {
            try
            {
                iCount = Nova_BLL_DepartMent.DeleteDepartMent(Nova_MOD_DepartMent.PID.ToString());
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
            gv_Bind();
        }
    }

    /// <summary>
    /// 删除多个
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnDel_Click(object sender, ImageClickEventArgs e)
    {
        StringBuilder sbID = new StringBuilder();
        for (int i = 0; i < this.gvDepart.Rows.Count; i++)
        {
            CheckBox chkSelect = (CheckBox)gvDepart.Rows[i].FindControl("chkSelect");
            if (chkSelect.Checked)
            {
                sbID.Append("'" + gvDepart.DataKeys[i].Value.ToString() + "',");
            }
        }
        if (sbID.Length > 0)
        {
            try
            {
                iCount = Nova_BLL_DepartMent.DeleteAllDepartMent(sbID.ToString().TrimEnd(','));
                if (iCount > 0)
                {
                    gv_Bind();
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
    }

    /// <summary>
    /// 行绑定
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvDepart_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            //ImageButton lbtnEdit = (ImageButton)e.Row.Cells[4].Controls[0];
            //Nova_BLL_BtnPermit.Scalarbtn(2, lbtnEdit, 2);//编辑按钮的权限

            //ImageButton ibtnDelete = (ImageButton)e.Row.Cells[5].FindControl("ibtnDelete");
            //Nova_BLL_BtnPermit.Scalarbtn(2, ibtnDelete, 1);//删除按钮的权限

        }
    }

    protected void gvDepart_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvDepart.PageIndex = e.NewPageIndex;
        gv_Bind(); //重新绑定GridView数据的函数
    }
}
